/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.feedback.thread;

import java.io.File;
import java.util.logging.Level;
import javax.swing.JOptionPane;
import project.jpdftools.animation.AnimationWorker;
import project.jpdftools.animation.AnimationWorkerThread;
import project.jpdftools.feedback.SendEmail;
import project.jpdftools.feedback.ZipFile;
import project.jpdftools.managers.LocalizationManager;
import project.jpdftools.managers.LoggerManager;

/**
 *
 * @author Mido
 */
public class FeedbackThread extends AnimationWorkerThread {

    private String email;
    private String feedback;
    private boolean allowAttachments;

    public FeedbackThread(AnimationWorker AnimationWorker, String email, String feedback, boolean allowAttachments) {
        super(AnimationWorker);
        this.email = email;
        this.feedback = feedback;
        this.allowAttachments = allowAttachments;
    }

    @Override
    public void run() {
        // start the animation
        this.getAnimationWorker().execute();

        // collect the information written and send it in an E-mail
        SendEmail send_email = new SendEmail("jpdf.tools@gmail.com", "@#^*$@(@JPDFTOOLS", email, "jpdf.tools@gmail.com",
                "EventLog.zip", "Feedback", feedback);
        boolean result = false;
        try {
            if (allowAttachments == true) {
                // compress the log file
                ZipFile zip_file = new ZipFile("EventLog");
                zip_file.Zip();

                // send the feedback and add the compressed log file as attachment
                result = send_email.sendEmailWithAttachment();

                // delete the compressed log file
                new File("EventLog.zip").delete();
                // delete the old log file
                // the program will create a new logger file in the next initialization
                new File("EventLog.txt").deleteOnExit();
            } else {
                // send feedback without attachment
                // send the feedback and add the compressed log file as attachment
                result = send_email.sendEmail();
            }
        } catch (Exception ex) {
            LoggerManager.getLogger().log(Level.SEVERE, "Exception occurred. Message = {0}", project.jpdftools.toolkit.Toolkit.getStackTraceAsString(ex.getCause()));
            result = false;
        }

        // kill the animation thread
        this.getAnimationWorker().setKillSignal(true);

        // set operation result
        // must be executed in the EventDispatchThread
        final boolean operationResult = result;
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                // show result
                if (operationResult == true) {
                    JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("FEEDBACK_UI_SEND_SUCCESS"),
                            LocalizationManager.getCurrent_resource().getString("SUCCESS"), 1);
                } else {
                    JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("FEEDBACK_UI_SEND_FAIL"),
                            LocalizationManager.getCurrent_resource().getString("ERROR"), 0);
                }
            }
        });
    }
}
